import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yj_health_plugin/yj_health_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  void initState() {
    super.initState();
    YjHealthPlugin.instance.messageCallback = ((String message){
      print("原生发送flutter消息--start");
      var jsonStr = message;
      Map<String, dynamic> arguments = json.decode(jsonStr);
      print("监听数据收到了");
      if (arguments.isNotEmpty) {
        print(arguments);
        print("我收到了消息");
      }
      print("原生发送flutter消息--end");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: ListView(
          children: [
            InkWell(
              onTap: () {
                Get.defaultDialog(
                    middleText: "是否通过服务UUID进行过滤",
                    title: "",
                    onConfirm: () async {
                      Get.back();
                      await YjHealthPlugin.instance.flutterSendEvent(flutterActionModel: FlutterActionModel(type: FlutterActionType.sendData, params: {
                        "name" : "scan_device",
                        "type" : 1,
                      }));
                    },
                    onCancel: () async {
                      await YjHealthPlugin.instance.flutterSendEvent(flutterActionModel: FlutterActionModel(type: FlutterActionType.sendData, params: {
                        "name" : "scan_device",
                        "type" : 0,
                      }));
                    }
                );
              },
              child: const ListTile(title: Text("搜索蓝牙"),),
            ),
            InkWell(
              onTap: () async {
                await YjHealthPlugin.instance.flutterSendEvent(flutterActionModel: FlutterActionModel(type: FlutterActionType.sendData, params: {
                  "name" : "stop_device",
                }));
              },
              child: const ListTile(title: Text("停止搜索设备"),),
            ),
            InkWell(
              onTap: () async {
                await YjHealthPlugin.instance.flutterSendEvent(flutterActionModel: FlutterActionModel(type: FlutterActionType.sendData, params: {
                  "name" : "connect",
                }));
              },
              child: const ListTile(title: Text("连接设备"),),
            ),
          ],
        )
    );
  }
}
